package com.kantboot.system.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.kantboot.business.water.repository.BusWaterOrderRepository;
import com.kantboot.functional.common.service.ICommonKantbootRsaService;
import com.kantboot.system.user.dao.repository.entity.SysUserRepository;
import com.kantboot.system.user.dao.repository.vo.SysUserVOOfSelfRepository;
import com.kantboot.system.user.domain.dto.InitSelfInfoDTO;
import com.kantboot.system.user.domain.vo.SysUserOfSelfVO;
import com.kantboot.system.user.exception.SystemUserException;
import com.kantboot.system.user.service.ISysTokenService;
import com.kantboot.system.user.service.IUserSelfService;
import com.kantboot.util.common.http.HttpRequestHeaderUtil;
import com.kantboot.util.common.password.KantbootPassword;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class UserSelfServiceImpl implements IUserSelfService {

    @Resource
    private SysUserRepository userRepository;

    @Resource
    private SysUserVOOfSelfRepository userVOOfSelfShowRepository;

    @Resource
    private HttpRequestHeaderUtil httpRequestHeaderUtil;

    @Resource
    private ISysTokenService tokenService;

    @Resource
    private KantbootPassword kantbootPassword;

    @Resource
    private ICommonKantbootRsaService kantbootRsaService;

    @Resource
    private BusWaterOrderRepository orderRepository;


    @Override
    public SysUserOfSelfVO getSelf() {
        Long selfId = getSelfId();
        return userVOOfSelfShowRepository.findById(selfId).orElseThrow(
                // 如果没有找到用户，抛出未登录异常
                () -> SystemUserException.NOT_LOGIN
        );
    }

    @Override
    public Long getSelfId() {
        String token = httpRequestHeaderUtil.getToken();
        Long userIdByToken = tokenService.getUserIdByToken(token);
        if (userIdByToken == null) {
            // 如果没有找到用户，抛出未登录异常
            throw SystemUserException.NOT_LOGIN;
        }
        return userIdByToken;
    }

    @Override
    public void changePassword(String oldPassword, String newPassword) {
        Long selfId = getSelfId();
        // 数据库中的密码
        String passwordById = userRepository.findPasswordById(selfId);
        if(!kantbootPassword.matches(oldPassword, passwordById)){
            // 如果原密码不正确，抛出原密码错误异常
            throw SystemUserException.OLD_PASSWORD_ERROR;
        }
        // 修改密码
        userRepository.updatePasswordById(selfId, kantbootPassword.encode(newPassword));
    }

    @Override
    public void changePasswordWithSecurity(String oldPassword, String newPassword) {
        String oldPasswordDecrypt = kantbootRsaService.decrypt(oldPassword);
        String newPasswordDecrypt = kantbootRsaService.decrypt(newPassword);
        changePassword(oldPasswordDecrypt, newPasswordDecrypt);
    }

    @Override
    public SysUserOfSelfVO initSelfInfo(InitSelfInfoDTO initSelfInfoDTO) {
        Long selfId = getSelfId();
        SysUserOfSelfVO self = BeanUtil.copyProperties(initSelfInfoDTO, SysUserOfSelfVO.class);
        self.setId(selfId);
        self.setIsInit(true);
        return userVOOfSelfShowRepository.save(self);
    }

}
